package com.gomoney.auth.security.config;

import com.gomoney.auth.security.service.UserDetailsServiceImpl;
import com.gomoney.auth.security.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.web.session.HttpSessionEventPublisher;

/**
 * @Description //TODO
 * @Author zhangyong
 * @Date 2021/4/23 15:42
 **/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

/*    @Autowired
    private FindByIndexNameSessionRepository<? extends Session> sessionRepository;*/

    @Autowired
    SessionRegistry sessionRegistry;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    /**
     * @Description //TODO
     * 因为我们用密码授权不需要登录了，它内部就需要一个认证管理器
     * @Author zhangyong
     * @Date 2021/4/25 14:55
     **/
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        // 全局配置：忽略url         urlPermissionsMap.put("/gomoney-auth/v2/api-docs", "anon");
        //        urlPermissionsMap.put("/gomoney-auth/v3/api-docs", "anon");
        web.ignoring().antMatchers("/sys/authentication/**", "/v2/api-docs", "/v3/api-docs");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 关闭跨站请求伪造
        //http.csrf().disable();
        http.formLogin();
        http.authorizeRequests().anyRequest().authenticated()
/*                .and()
                .requestCache()
                .requestCache(new NullRequestCache())*/
                .and().httpBasic()
                .and()
                .csrf().disable();
                /*.csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()*/
               /* .sessionManagement()
                .maximumSessions(1)
                .maxSessionsPreventsLogin(true).sessionRegistry(getSessionRegistry());*/
        //.expiredSessionStrategy(new CustomSessionInformationExpiredStrategy());
/*        http.headers()
                .contentTypeOptions()
                .and()
                .xssProtection()
                .and()
                //禁用缓存
                .cacheControl()
                .and()
                .httpStrictTransportSecurity()
                .and()
                //禁用页面镶嵌frame劫持安全协议  // 防止iframe 造成跨域
                .frameOptions().disable();*/
    }

/*    @Bean
    SpringSessionBackedSessionRegistry sessionRegistry() {
        return new SpringSessionBackedSessionRegistry(sessionRepository);
    }*/

    /*@Bean
    public SpringSessionBackedSessionRegistry sessionRegistry(){
        return new SpringSessionBackedSessionRegistry<>(sessionRepository);
    }*/

    @Bean
    public SessionRegistry getSessionRegistry() {
        SessionRegistry sessionRegistry = new SessionRegistryImpl();
        return sessionRegistry;
    }

    @Bean
    public ServletListenerRegistrationBean httpSessionEventPublisher() {
        return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());
    }
}
